package com.zr.quartz.task;

import com.zr.command.domain.EimCommandSchedule;
import com.zr.command.service.IEimCommandScheduleService;
import com.zr.common.core.domain.entity.SysUser;
import com.zr.common.utils.SecurityUtils;
import com.zr.eim.domain.EimHomeInformation;
import com.zr.eim.domain.EimOuterCitizen;
import com.zr.eim.service.IEimDataHistoryService;
import com.zr.eim.service.IEimHomeInformationService;
import com.zr.eim.service.IEimOuterCitizenService;
import com.zr.resident.domain.EimMassesinfoNucleicposition;
import com.zr.resident.service.IEimMassesinfoNucleicpositionService;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import javax.mail.MessagingException;
import java.util.List;


/**
 * 定时任务调度
 *  每天定时扫描居民信息表，给处于居家监测期间的居民创建核酸检测表
 * @author chengkai
 */
@Component("eimTask")
public class EimTask {

    @Resource
    private IEimHomeInformationService eimHomeInformationService;

    @Resource
    private IEimCommandScheduleService eimCommandScheduleService;

    @Resource
    private IEimDataHistoryService eimDataHistoryService;

    @Resource
    private IEimMassesinfoNucleicpositionService eimMassesinfoNucleicpositionService;

    @Resource
    private IEimOuterCitizenService eimOuterCitizenService;

    @Resource
    private JavaMailSender mailSender;

    /**
     * 每天00：00：00定时执行一次居家监测表扫描任务
     */
    @Transactional
    public void eimHomeTest(){
        eimHomeInformationService.removeMonitorList();//解除居家监测到期的居民的居家状态
        List<EimHomeInformation> list= eimHomeInformationService.selectEimTaskList();//查询处于居家监测状态中的居民列表
        for(EimHomeInformation information : list){
            EimCommandSchedule schedule = new EimCommandSchedule();
            schedule.setTitle("核酸检测工单");
            schedule.setType("2");
            schedule.setDescription("请尽快对确诊病例完成核酸检测");
            schedule.setIdCard(information.getIdCard());
            eimCommandScheduleService.insertEimCommandSchedule(schedule);
        }
    }

    /**
     * 每月最后一天23：59：55秒定时执行一次疫情统计信息更新
     */
    public void eimHistoryNum(){
        eimDataHistoryService.updateEimDataHistory();//解除居家监测到期的居民的居家状态
    }

    /**
     * 每10秒执行一次核酸监测热度更新任务
     */
    public void eimNucleicAcidCheck(){
        EimMassesinfoNucleicposition position=new EimMassesinfoNucleicposition();
        eimMassesinfoNucleicpositionService.updateEimMassesinfoNucleicposition1(position);
    }

    /**
     * 每30分钟扫描一次指挥工单表，对即将到时间且尚未分配的工单给区疾控中心管理员发邮件提醒
     */
    public void sendMessage() throws MessagingException {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        if(user!=null){
            String toEmail = user.getEmail();
            List<EimCommandSchedule> list=eimCommandScheduleService.listUndispatchedEimCommandSchedule(user);
            for (EimCommandSchedule schedule:list){
                SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
                simpleMailMessage.setFrom("17519468725@163.com");
                simpleMailMessage.setTo(toEmail);
                simpleMailMessage.setSubject("紧急待办任务:"+schedule.getTitle());
                simpleMailMessage.setText("任务对象:"+schedule.getEimDataPerson().getName()+"/n证件号:"+schedule.getIdCard()+"/n任务描述:"+schedule.getDescription());
                mailSender.send(simpleMailMessage);
            }
        }
    }

    /**
     * 每天00：00：00定时执行一次外部输入居民信息录入工作
     */
    public void inputOuterCitizenInfo(){
        eimOuterCitizenService.outerCitizenToDataPerson();
    }

    /**
     * 每隔15分钟定时执行扫描一次外部输入人员信息表
     */
    public void outerCitizenScanTask(){
        List<EimOuterCitizen> list=eimOuterCitizenService.listExceptionOuterCitizen();
        for(EimOuterCitizen citizen:list){
            eimCommandScheduleService.insertEimCommandScheduleByOuterCitizen(citizen);
        }
    }

}
